package com.hightemperator.service;

import com.hightemperator.dao.*;
import com.hightemperator.po.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class HomePageService {

    @Autowired
    public BannerMapper bannerMapper;
    @Autowired
    public SysUserMapper sysUserMapper;
    @Autowired
    public ActivityMapper activityMapper;
    @Autowired
    public CircleMapper circleMapper;
    @Autowired
    public JoinActivityMapper joinActivityMapper;
    @Autowired
    public SysUserAttentionMapper sysUserAttentionMapper;
    @Autowired
    public MessageMapper messageMapper;

    /**
     * 获取轮播图片列表
     *
     * @return
     */
    public List<Banner> getBanner() {
        BannerExample example = new BannerExample();
        return bannerMapper.selectByExample(example);
    }

    /**
     * 获取VIP列表
     *
     * @return
     */
    public List<SysUser> getVIPUser() {
        Map<String, Object> param = new HashMap<>();
        param.put("begin", 0);
        param.put("end", 44);
        param.put("isvip", "1");
        return sysUserMapper.getVipUserList(param);
    }


    /**
     * 高级查询
     *
     * @param city
     * @param sex
     * @param age
     * @param hobby
     * @param heightMin
     * @param heightMax
     * @param weightMin
     * @param weightMax
     * @param nativeplace
     * @return
     */
    public List<Integer> getEligibilityUser(String city, String sex, String age, String hobby,
                                            String heightMin, String heightMax, String weightMin,
                                            String weightMax, String nativeplace) {
        Map<String, Object> map = new HashMap<>();
        map.put("city", city);
        map.put("sex", sex);
        map.put("age", age);
        map.put("hobby", hobby);
        map.put("heightMin", heightMin);
        map.put("heightMax", heightMax);
        map.put("weightMin", weightMin);
        map.put("weightMax", weightMax);
        map.put("nativeplace", nativeplace);
        return sysUserMapper.getEligibilityUser(map);
    }

    /**
     * 根据用户id获取用户活动列表
     *
     * @return
     */
    public List<ActivityAndUserMessage> getActivityListById(Integer id, Integer begin, Integer end) {
        return activityMapper.getActivityMessageForId(id, begin, end);
    }


    /**
     * 根据用户id获取用户动态列表
     *
     * @param id
     * @return
     */
    public List<CircleAndUserMessage> getCircleListById(Integer id, Integer begin, Integer end) {
        return circleMapper.getCircleListForId(id, begin, end);
    }

    /**
     * 获取首页动态列表
     * 以及高级查询
     *
     * @return
     */
    public List<ActivityAndUserMessage> getAcitvityList(Integer uId, String city, String sex, String age, String hobby,
                                                        String heightMin, String heightMax, String weightMin,
                                                        String weightMax, String nativeplace, Integer begin, Integer end) {
        Map<String, Object> param = new HashMap<>();
        param.put("begin", begin);
        param.put("end", end);
        param.put("id", uId);
        param.put("city", city);
        param.put("sex", sex);
        param.put("age", age);
        param.put("hobby", hobby);
        param.put("heightMin", heightMin);
        param.put("heightMax", heightMax);
        param.put("weightMin", weightMin);
        param.put("weightMax", weightMax);
        param.put("nativeplace", nativeplace);
        return activityMapper.getActivityMessageList(param);
    }

    /**
     * 获取首页动态列表
     *
     * @return
     */
    public List<CircleAndUserMessage> getCircleMessageList(Integer uId, String city, String sex, String age, String hobby,
                                                           String heightMin, String heightMax, String weightMin,
                                                           String weightMax, String nativeplace, Integer begin, Integer end) {
        Map<String, Object> param = new HashMap<>();
        param.put("begin", begin);
        param.put("end", end);
        param.put("id", uId);
        param.put("city", city);
        param.put("sex", sex);
        param.put("age", age);
        param.put("hobby", hobby);
        param.put("heightMin", heightMin);
        param.put("heightMax", heightMax);
        param.put("weightMin", weightMin);
        param.put("weightMax", weightMax);
        param.put("nativeplace", nativeplace);
        return circleMapper.getCircleMessageList(param);
    }

    /**
     * 加入活动
     * 加入活动 实际就是给活动发起者添加一条消息 如果对方同意才可以加入活动
     * 相互关注 确认需求之后在说
     * 大概逻辑为 相互确认是否为关注后 不是关注进行关注操作 在此之前 进行加入活动操作
     * 查询 是否已有曾经加入这个活动的记录 如果有 加入失败
     * 发送的消息的id 和 昵称需要到对应的表中查询
     *
     * @param joinActivity
     * @return
     */
    public int updateJoinActivity(JoinActivity joinActivity) {

        int successCode = 0;
        JoinActivityExample example = new JoinActivityExample();
        JoinActivityExample.Criteria criteria = example.createCriteria();
        criteria.andActivityidEqualTo(joinActivity.getActivityid());
        criteria.andUseridEqualTo(joinActivity.getUserid());
        int count = joinActivityMapper.countByExample(example);

        if (count == 0) {
            successCode = joinActivityMapper.insertSelective(joinActivity);
        }

        //双方关注逻辑，暂时注释
        //查询双方是否关注过，如果没关注自动关注
        if (successCode != 0) {
//            Map<String, Object> map = new HashMap<>();
//            map.put("userId", joinActivity.getCreateuserid());
//            map.put("attentionId", joinActivity.getUserid());
//            if (sysUserAttentionMapper.getAttention(map) == 0) {
//                SysUserAttention sysUserAttention = new SysUserAttention();
//                sysUserAttention.setUserid(joinActivity.getCreateuserid());
//                sysUserAttention.setAttentionid(joinActivity.getUserid());
//                if (sysUserAttentionMapper.insertSelective(sysUserAttention) == 0) {
//                    return 0;
//                }
//            }
//            Map<String, Object> map2 = new HashMap<>();
//            map2.put("userId", joinActivity.getUserid());
//            map2.put("attentionId", joinActivity.getCreateuserid());
//            if (sysUserAttentionMapper.getAttention(map2) == 0) {
//                SysUserAttention sysUserAttention = new SysUserAttention();
//                sysUserAttention.setUserid(joinActivity.getUserid());
//                sysUserAttention.setAttentionid(joinActivity.getCreateuserid());
//                if (sysUserAttentionMapper.insertSelective(sysUserAttention) == 0) {
//                    return 0;
//                }
//            }

            //查询出来当前接口之前插入的数据的id
            Map<String, Object> param = new HashMap<>();
            param.put("userid", joinActivity.getUserid());
            param.put("activityid", joinActivity.getActivityid());
            List<JoinActivityUsedMessage> joinActivities = joinActivityMapper.JoinActivityUsedMessage(param);
            JoinActivityUsedMessage joinActivityUsedMessage = joinActivities.get(0);
            String str = joinActivityUsedMessage.getActivityUserMessage().getNickname() + "," + "想参加您的活动";
            insertMessage(joinActivity.getUserid(), joinActivity.getCreateuserid(), joinActivityUsedMessage.getActivityid(), 0, str, "1");
        }
        return successCode;
    }

    /**
     * 插入 消息
     *
     * @param reciveId       消息发送者的id
     * @param creatorId      消息用户的id
     * @param type           插入消息的类型
     * @param circleId       圈子关联id
     * @param commendId      回复关联表id
     * @param messageContent 消息内容
     * @return
     */
    public int insertMessage(Integer reciveId, Integer creatorId, Integer circleId, Integer commendId,
                             String messageContent, String type) {

        Message message = new Message();
        message.setCreatorid(creatorId);
        message.setCircleid(circleId);
        message.setCommendid(commendId);
        message.setContent(messageContent);
        message.setType(type);
        message.setReciveid(reciveId);
        int successCode = messageMapper.insertSelective(message);

        return successCode;
    }

}